package com.example.win.android_banduyujia.peopleactivity.activity.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.Constants;
import com.example.win.android_banduyujia.http.request.ChangePasswordRequest;
import com.example.win.android_banduyujia.http.response.BaseHttpResponse;
import com.example.win.android_banduyujia.lib.util.StrUtils;
import com.example.win.android_banduyujia.login.application.LoginApplication;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Win on 2015/8/21.
 */
public class ChangePwdActivity extends BaseTitleBarActivity {
    public static final String  TAG = ChangePwdActivity.class.getSimpleName();

    private EditText  mEditoldpwd, mEditnewpwd, mEditnewagainpwd;

    private  String  mOldpwd,mNewpwd,mNewpwdAgain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_change_pwd);

        InitTitles();
        InitViews();
    }

    private void InitTitles() {
       showLeftBackButton();
       setTitle(R.string.text_view_submit_audit_fix_password);
        setRightTextViewListener(R.string.text_view_submit_audit_finish, mConfirmListener);
    }

    private View.OnClickListener mConfirmListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(checkUserEnter()){
                ChangePasswordRequest request = new ChangePasswordRequest(StrUtils.createUUID(), mOldpwd, mNewpwdAgain, LoginApplication.getInstance().getAccessToken());
                mAsycHttpRequest.doRequest(ChangePwdActivity.this, request, BaseHttpResponse.class, mChangePwdHandler, true);
            }
        }
    };

    private Handler mChangePwdHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            showToast("密码修改成功");
            finish();
        }
    };

    private void InitViews() {
        mEditoldpwd = (EditText) findViewById(R.id.change_name_old_edit_text);
        mEditnewpwd = (EditText) findViewById(R.id.change_name_new_edit_text);
        mEditnewagainpwd = (EditText) findViewById(R.id.change_name_new_again_edit_text);
    }

    private boolean checkUserEnter() {
        mOldpwd = mEditoldpwd.getText().toString();
        mNewpwd = mEditnewpwd.getText().toString();
        mNewpwdAgain=mEditnewagainpwd.getText().toString();

        if(StrUtils.isEmpty(mOldpwd)){
            showToast(R.string.change_pwd_activity_enter_old_pwd);
            return false;
        }
        if(mOldpwd.length() < Constants.PASSWORD_MIN_LENGTH){
            showToast(R.string.change_pwd_activity_old_pwd_too_short);
            return false;
        }
        if(StrUtils.isEmpty(mNewpwd)){
            showToast(R.string.change_pwd_activity_new_pwd);
            return false;
        }
        if(mNewpwd.length() < Constants.PASSWORD_MIN_LENGTH){
            showToast(R.string.change_pwd_activity_old_pwd_too_short);
            return false;
        }
        if(StrUtils.isEmpty(mNewpwdAgain)){
            showToast(R.string.change_pwd_activity_enter_new_pwd_again);
            return false;
        }
        if(!mNewpwd.equals(mNewpwdAgain)){
            showToast(R.string.change_pwd_activity_diff_pwd);
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
